package com.example.demo.controller;

import com.ejlchina.searcher.MapSearcher;
import com.ejlchina.searcher.SearchResult;
import com.ejlchina.searcher.util.MapUtils;
import com.example.demo.entity.pojo.PictureInfo;
import com.example.demo.service.UniversalService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * The type Picture controller.
 * 操作图片的Controller
 *
 * @author Peng
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/utils")
public class PictureController {

    private final UniversalService service;
    private final MapSearcher mapSearcher;

    /**
     * Get info by picture object.
     * 根据图片获得图片具体信息
     *
     * @param file the file
     * @return the object
     */
    @GetMapping("/picture")
    public PictureInfo getInfoByPicture(MultipartFile file) throws Exception {
        return service.readImageInfo(file);
    }

    @GetMapping
    public SearchResult<Map<String, Object>> index(HttpServletRequest request) {
        return mapSearcher.search(PictureInfo.class, MapUtils.flat(request.getParameterMap()));
    }
}
